package com.wangdao.wechatmall.controller.admin;

import com.wangdao.wechatmall.bean.bo.admin.AdminCreateBO;
import com.wangdao.wechatmall.bean.bo.admin.AdminParamBO;
import com.wangdao.wechatmall.bean.bo.admin.AdminUpdateBO;
import com.wangdao.wechatmall.bean.vo.admin.AdminCreateVO;
import com.wangdao.wechatmall.bean.vo.admin.AdminDataVO;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.exception.AdminDuplicatedException;
import com.wangdao.wechatmall.exception.AdminEmptyException;
import com.wangdao.wechatmall.exception.AdminPassWordException;
import com.wangdao.wechatmall.exception.AdminUserNameException;
import com.wangdao.wechatmall.service.admin.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 管理员模块
 * @ClassName: AdminController
 * @Description 管理员类
 * @Author Xinhao Yi
 * @Date 2021/7/8  20:42
 */
@RestController
@RequestMapping("admin/admin")
public class AdminControlController {
    @Autowired
    AdminService adminService;

    //请求 URL: http://localhost:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc
    //相应负载：{"errno":0,"data":{"total":12,"items":[{"id":17,"username":"ffffffdd","avatar":"'","roleIds":[1,2]},{"id":16,"username":"A1saad","avatar":"'","roleIds":[10]},{"id":15,"username":"asdqwe","avatar":"'","roleIds":[1,2,3]},{"id":14,"username":"shtttt","avatar":"'","roleIds":[1,2,3]},{"id":13,"username":"132456","avatar":"'","roleIds":[10]},{"id":12,"username":"Asdadsa","avatar":"'","roleIds":[10,3]},{"id":11,"username":"admin567","avatar":"'","roleIds":[1,2]},{"id":8,"username":"DaasA2","avatar":"'","roleIds":[10,11]},{"id":6,"username":"yongganniuniu","avatar":"'","roleIds":[1]},{"id":5,"username":"mall123","avatar":"'","roleIds":[2]},{"id":4,"username":"promotion123","avatar":"'","roleIds":[3]},{"id":1,"username":"admin123","avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif","roleIds":[1]}]},"errmsg":"成功"}
    /**
     * 拿到所有的admin管理员
     */
    @RequestMapping("/list")
    public BaseRespVo list(AdminParamBO param) {
        AdminDataVO adminDataVO = adminService.queryAdmin(param);
        return BaseRespVo.ok(adminDataVO);
    }

    //todo 图片上传还没有处理
    //请求 URL: http://localhost:8083/admin/admin/create
    //请求负载：{"username":"Yxh1234","password":"123456","avatar":"http://182.92.235.201:8083/wx/storage/fetch/vu0cb1etwbz1rcknmyfj.jpg","roleIds":[1,2,11]}
    //响应负载：{"errno":0,"data":{"id":29,"username":"Yxh1234","password":"$2a$10$FrlYZizSgeOhFfd2XngNZu0R6xSWCROoCQttn07vPt/5yoENGP8DG","avatar":"http://182.92.235.201:8083/wx/storage/fetch/vu0cb1etwbz1rcknmyfj.jpg","addTime":"2021-07-09 02:38:44","updateTime":"2021-07-09 02:38:44","roleIds":[1,2,11]},"errmsg":"成功"}
    /**
     * 创建管理员
     * @param param
     * @return
     */
    @RequestMapping("/create")
    public BaseRespVo create(@RequestBody AdminCreateBO param, HttpServletRequest httpServletRequest) throws AdminUserNameException, AdminDuplicatedException, AdminPassWordException, AdminEmptyException {
        AdminCreateVO adminCreateVO = adminService.createAdmin(param);
        return BaseRespVo.ok(adminCreateVO,"添加成功");
    }

    //todo 图片上传还没有处理
    //请求 URL: http://localhost:8083/admin/admin/update
    //请求负载：{"id":41,"username":"hanson","avatar":"http://182.92.235.201:8083/wx/storage/fetch/oi45wgxrq4fl7xp8uh8y.jpeg","roleIds":[3,10],"password":"yxh123456"}
    //响应负载：{"errno":0,"data":{"id":41,"username":"hanson","password":"$2a$10$6qrBCJNLfl1IozdFjYUD/uholwQXkwDFEdRVYrNChKL/9ivvwNq0S","avatar":"http://182.92.235.201:8083/wx/storage/fetch/oi45wgxrq4fl7xp8uh8y.jpeg","updateTime":"2021-07-09 13:56:10","roleIds":[3,10]},"errmsg":"成功"}

    /**
     * 编辑管理员
     * @param param
     * @return
     * @throws AdminEmptyException
     * @throws AdminDuplicatedException
     * @throws AdminPassWordException
     * @throws AdminUserNameException
     */
    @RequestMapping("/update")
    public BaseRespVo update(@RequestBody AdminUpdateBO param, HttpServletRequest httpServletRequest) throws AdminEmptyException, AdminDuplicatedException, AdminPassWordException, AdminUserNameException {
        AdminCreateVO adminCreateVO = adminService.update(param);
        return BaseRespVo.ok(adminCreateVO,"更新成功");
    }


    //请求 URL: http://localhost:8083/admin/admin/delete
    //请求负载：{"id":28,"username":"123213","avatar":"http://182.92.235.201:8083/wx/storage/fetch/aq7wbuweznxz602s4vam.png","roleIds":[10,3,2]}
    //响应负载：{"errno":0,"errmsg":"成功"}
    @RequestMapping("/delete")
    public BaseRespVo delete(@RequestBody AdminUpdateBO param, HttpServletRequest httpServletRequest){
        adminService.delete(param);
        return BaseRespVo.ok(null,"删除成功");
    }



}
